let http = require('http');
let cheerio = require('cheerio');
let request = require('request');
let fs = require('fs');

let result = {
  http: [],
  https: []
}

// 爬取首页
function getIndex(url) {
  request(url, (err, res, body) => {
    if (err) {
      console.log(`爬取首页出错`);
      return;
    } else {
      let $ = cheerio.load(body);
      $('#ip_list').find('tr').each((idx, element) => {
        let $element = $(element);
        let type = $element.find('td').eq(5).text();
        if (type === 'HTTP') {
          result.http.push(`${$element.find('td').eq(1).text()}:${$element.find('td').eq(2).text()}`)
        } else if (type === 'HTTPS') {
          result.https.push(`${$element.find('td').eq(1).text()}:${$element.find('td').eq(2).text()}`)
        }
      })
      console.log(result.http);
      saveText(result.http, 'http');
      saveText(result.https, 'https');
    }
  })
}

// 保存文本
function saveText(obj, type) {
  let txt = JSON.stringify(obj);
  console.log(txt);
  fs.unlink(`./ip_${type}.json`, (err) => {
    if (err) {
      console.log(`删除文件出错,错误原因${err}`);
    }
    fs.appendFile(`./ip_${type}.json`, txt, 'utf-8', (err) => {
      if (err) {
        console.log(`保存出错`);
      } else {
        console.log(`完成-------------------------`);
      }
    })
  })
}

// 目标
let url = 'http://www.xicidaili.com/nn/';
getIndex(url);